import { CATEGORYINFO, PROUCTINFO, ATTRIBUTE, GET_PROUCTS, GET_CATEGORYS, CATEGORYSON } from '../mutations-type'
import { updateProducts, getCategoryinfo, addProducts, getProductInfos, searchAttributes, addAttributes, updateAttributes, delAttributes, getAttributes, updateStatus, getProducts, psearchs, getCategorys, updateCategorys, getCategorysons, addCategorys } from '../../api/product'
const products = {
    state: {
        products: {
        },
        categorys: {
        },
        categorysons: {
        },
        attributes: {
        },
        productinfo: {
        },
        cateinfo: {
        },

    },
    mutations: {
        [GET_PROUCTS](state, obj) {
            state.products = obj
        },
        [GET_CATEGORYS](state, obj) {
            state.categorys = obj
        },
        [CATEGORYSON](state, obj) {
            state.categorysons = obj
        },
        [ATTRIBUTE](state, obj) {
            state.attributes = obj
        },
        [PROUCTINFO](state, obj) {
            state.productinfo = obj
        },
        [CATEGORYINFO](state, obj) {
            state.cateinfo = obj
        },




    },
    actions: {

        async addProducts(_, obj) {
            let orders = await addProducts(obj)
            console.log(orders)
            // commit('GET_PROUCTS', orders.data.data)
        },

        async updateProducts(_, obj) {
            let orders = await updateProducts(obj)
            console.log(orders)
            // commit('GET_PROUCTS', orders.data.data)
        },
        async getProducts({ commit }, obj) {
            let orders = await getProducts(obj)
            console.log(orders)
            commit('GET_PROUCTS', orders.data.data)
        },
        async getProductInfos({ commit }, obj) {
            let products = await getProductInfos(obj)
            console.log(products)
            commit('PROUCTINFO', products.data.data)
        },

        async psearchs({ commit }, obj) {
            let products = await psearchs(obj)
            console.log(obj, products)
            commit('GET_PROUCTS', products.data.data)
        },
        async getCategorys({ commit }, obj) {
            let categorys = await getCategorys(obj)
            commit('GET_CATEGORYS', categorys.data.data)
        },
        async updateCategorys(_, obj) {
            let categorys = await updateCategorys(obj)
            if (categorys.data.status == 0) {
                location.reload()
            }

        },
        //根据分类ID获取分类
        async getCategoryinfo({ commit }, obj) {
            let categorys = await getCategoryinfo(obj)
            commit('CATEGORYINFO', categorys.data.data)

        },

        async getCategorysons({ commit }, obj) {
            let categorys = await getCategorysons(obj)
            commit('CATEGORYSON', categorys.data.data)

        },
        async addCategorys(_, obj) {
            let categorys = await addCategorys(obj)
            console.log(categorys)
        },
        async updateStatus(_, obj) {
            await updateStatus(obj)

        },

        // 规格
        async getAttributes({ commit }, obj) {
            let attributes = await getAttributes(obj)

            var da = attributes.data.data.list
            da.forEach((item) => {
                getCategoryinfo(item.category_id).then((da) => {
                    item.categoryname = da.data.data.name;
                });
            });
            
            commit('ATTRIBUTE', attributes.data.data)

        },
        async delAttributes(_, obj) {
            let attributes = await delAttributes(obj)
            if (attributes.data.status == 0) {
                alert("删除成功", location.reload())
            }
        },
        async updateAttributes(_, obj) {
            await updateAttributes(obj)
        },
        async addAttributes(_, obj) {
            console.log(obj, 999)
            let attributes = await addAttributes(obj)
            console.log(attributes, 1000)
        },
        async searchAttributes({ commit }, obj) {
            console.log(obj, 999)
            let attributes = await searchAttributes(obj)
            commit('ATTRIBUTE', attributes.data.data)
        },


    }

}

export default products